{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['./testall.txt']\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import re\n",
    "import sys\n",
    "import codecs\n",
    "import datetime\n",
    "\n",
    "def load_files(files):\n",
    "\t'''\n",
    "\t:param files:文件列表\n",
    "\t:return:文件内容\n",
    "\t'''\n",
    "\ttext = []\n",
    "\tprint(files)\n",
    "\tfor file in files:\n",
    "\t\tif file:\n",
    "\t\t\twith codecs.open(file, 'rb', encoding='utf-8') as f:\n",
    "\t\t\t\ttext.extend(f.readlines())\n",
    "\treturn text\n",
    "text = load_files(['./testall.txt'])\n",
    "regex=re.compile('.+/nr')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['19980101-01-001-002/m', '中共中央/nt', '总书记/n', '、/w', '国家/n', '主席/n', '江/nr', '泽民/nr', '和/p', '迪丽/nr', '热巴/nr', './w\\n']\n",
      "12\n"
     ]
    }
   ],
   "source": [
    "sent=text[1]\n",
    "tokens=sent.split('  ')\n",
    "print(tokens)\n",
    "print(len(tokens))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6 7 ['江/nr', '泽民/nr']\n",
      "9 10 ['迪丽/nr', '热巴/nr']\n"
     ]
    }
   ],
   "source": [
    "groups=[]\n",
    "first_index=-1\n",
    "last_index=-1\n",
    "i=0\n",
    "j=0\n",
    "while i<len(tokens):\n",
    "    t=tokens[i]\n",
    "    if regex.match(t):\n",
    "        if first_index==-1:#找到块的第一个时间\n",
    "            first_index=i\n",
    "            j=0\n",
    "            while j<len(tokens[i+1:]):\n",
    "                tt=tokens[i+1+j]\n",
    "                if regex.match(tt):\n",
    "                    last_index=i+1+j#找到块的最后一个时间\n",
    "                    j+=1\n",
    "                else:\n",
    "                    last_index=i+j#找到块的最后一个时间\n",
    "                    break\n",
    "            print(first_index,last_index,tokens[first_index:last_index+1])\n",
    "            groups.append((first_index,last_index))\n",
    "            if last_index==-1:\n",
    "                break\n",
    "            i=last_index\n",
    "            first_index=-1\n",
    "            last_index=-1\n",
    "    i+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(6, 7), (9, 10)]"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "groups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['江泽民/nr', '迪丽热巴/nr']\n"
     ]
    }
   ],
   "source": [
    "merges=[]\n",
    "for i,g in enumerate(groups):\n",
    "    a,b=g\n",
    "    time_tokens=tokens[a:b+1]\n",
    "    merge = ''\n",
    "    for t in time_tokens:\n",
    "        merge += t[:t.index('/nr')]\n",
    "    merge += '/nr'\n",
    "    merges.append(merge)\n",
    "print(merges)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "merges.reverse()\n",
    "for i,g in enumerate(groups[::-1]):\n",
    "    a,b=g\n",
    "    tokens[a:b+1]=[merges[i]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['19980101-01-001-002/m',\n",
       " '中共中央/nt',\n",
       " '总书记/n',\n",
       " '、/w',\n",
       " '国家/n',\n",
       " '主席/n',\n",
       " '江泽民/nr',\n",
       " '和/p',\n",
       " '迪丽热巴/nr',\n",
       " './w\\n']"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokens"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "s='abc///ads'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.index('/')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'abc'"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s[:s.index('/')]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
